/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import edu.pku.sei.modeler.editpolicies.UMLDiagramGraphicalNodeEditPolicy;
import edu.pku.sei.modeler.figures.ActorFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.UseCases.Actor;
import edu.pku.sei.modeler.uml.commands.ChangeNameCommand;

/**
 * @generated
 * 
 */
public class ActorEditpart extends AbstractUMLNodeEditPart {
	public ActorEditpart(MetaShapeElement model) {
		super(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		// ActorFigure figure = new ActorFigure();

		return new ActorFigure();
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return new DirectEditPolicy() {
			protected Command getDirectEditCommand(DirectEditRequest request) {
				return new ChangeNameCommand(getModelElement(),
						(String) request.getCellEditor().getValue());
			}

			protected void showCurrentEditValue(DirectEditRequest request) {
				IFigure fig = getDirectEditFigure();
				if (fig instanceof Label) {
					((Label) fig).setText(getModelElement().getName());
					fig.getUpdateManager().performUpdate();
				}
			}
		};

	}

	@Override
	protected IFigure getDirectEditFigure() {
		return ((ActorFigure) this.getFigure()).getActorNameLabel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		// String propertyName = event.getPropertyName();
		refreshVisuals();
		super.handlePropertyChanged(event);
	}

	private Actor getModelElement() {
		return (Actor) ((MetaNode) getModel()).getModelElement();
	}

	//	
	protected void refreshVisuals() {
		Actor modelEle = getModelElement();
		ActorFigure figure = (ActorFigure) getFigure();

		System.out.println("figure:" + figure.getBounds());

		figure.setActorName(modelEle.getName());

		super.refreshVisuals();

	}
}
